<template>
  <div class="elm-edit">
    <elm-page-bb>
      <el-form ref="model" :model="model" size="small" label-width="120px">
        <elm-row>
          <el-form-item label="物资名称" prop="material_name" :rules="[{ required: true, message: '必须填写'}]">
            <elm-select-input :show-id="true" :code-array="selectCode" v-model="model.material_name" url="/business/wms/goods/GetSelectData" width="600px"></elm-select-input>
          </el-form-item>
          <el-form-item label="物资类别" prop="material_category_mc">
            <el-input v-model="model.material_category_mc" disabled></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="规格/型号" prop="specification">
            <el-input v-model="model.specification" disabled></el-input>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-input v-model="model.unit" disabled></el-input>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="当前库存数量" prop="stock_quantity" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model="model.stock_quantity" disabled>
              <template slot="append"><div style="width: 25px;text-align: center;">{{model.unit}}</div></template>
            </el-input>
          </el-form-item>
          <el-form-item label="库存状态" prop="stock_status">
            <elm-code dict_type="wms_material_ss" v-model="model.stock_status"></elm-code>
          </el-form-item>
        </elm-row>
        <elm-row>
          <el-form-item label="安全库存量" prop="safety_stock_quantity" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model="model.safety_stock_quantity">
              <template slot="append"><div style="width: 25px;text-align: center;">{{model.unit}}</div></template>
            </el-input>
          </el-form-item>
          <el-form-item label="最大库存量" prop="max_stock_quantity" :rules="[{trigger: 'blur' ,validator: validateNumber}]">
            <el-input v-model="model.max_stock_quantity">
              <template slot="append"><div style="width: 25px;text-align: center;">{{model.unit}}</div></template>
            </el-input>
          </el-form-item>
        </elm-row>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="model.remarks" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
      <template #bottom>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      model: {
        id: elm.newGuid(),
        unit:null,
        specification:null,
        material_category:null,
        material_category_mc:null,
      },
      selectCode:[
        {
          label:'规格/型号',
          prop:'specification'
        },
      ],
    }
  },
  async created() {
    if(this.model.stock_quantity==null||this.model.stock_quantity==undefined)
      this.model.stock_quantity=0;
  },
  destroyed() {
    this.request.callVue.tablebinding();
  },
  methods: {

    async submit() {
      await this.$refs["model"].validate();
      var res = await elm.post('/business/wms/material/Upsert', this.model);
      if (res == true) {
        this.v('保存成功。')
        this.close();
        this.request.callVue.tablebinding();
      }
      else {
        this.error('保存失败，请稍后尝试。');
      }
    },
  },
  watch: {
    'model.material_name': async function(newVal) {
      //console.log(`material_name changed to: ${newVal}`);
      var res=await elm.post('/business/wms/goods/GetGoodsData', {id:newVal});
      //console.log("结果：",res);
      if(res.length>0){
        Object.assign(this.model, res[0]);
      }
      //console.log(this.model)
    }
  }
}
</script>